"""
@author: peng
@email: sb.peng@hzgosun.com
@file: camera_style_manageTest.py
@time: 2019/5/10 16:14

"""
import unittest
from time import sleep
from selenium.webdriver.common.keys import Keys
from common.functions import save_screenshot
from common.decorators import mkdir_classname, log4gs
from common.settings import TAG
from common.vue.select import DropDownSelect,OperateSelect
from pages.cameraStyleManagePage import CamerastylemangePage


@log4gs
@mkdir_classname(TAG)
class Camera(unittest.TestCase):
    """摄像机管理功能测试"""
    @classmethod
    def setUpClass(cls):
        cls.log.debug('='*30 + '开始摄像机管理功能测试' + '='*30)
        cls.page =CamerastylemangePage('/DevManage/camTypeManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束摄像机管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))

    #@unittest.skip
    def test_1_page(self):
        """翻页测试"""
        sleep(1)
        self.log.debug("首页共有：{}页数据".format(self.page.turnPage.total_page))
        self.log.debug("当前处于第：{}页数据".format(self.page.turnPage.current_page))
        self.log.debug("共有：{}条数据".format(self.page.turnPage.total_num))
        sleep(2)
        self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
        try:
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            try:
                self.page.turnPage.next_page.click()
                self.assertEqual(self.page.turnPage.current_page, '3')
                sleep(2)
                self.page.turnPage.prev_page.click()
                self.assertEqual(self.page.turnPage.current_page, '2')
                sleep(2)
                self.page.turnPage.input.clear()
                self.page.turnPage.input.send_keys(3)
                self.page.turnPage.input.send_keys(Keys.ENTER)
                self.assertEqual(self.page.turnPage.current_page, '3')
            except Exception as e:
                print("无法执行翻页测试" % e)
        except Exception as e:
            print("用例执行失败:%r" % e)
            save_screenshot("翻页功能测试失败")

    #@unittest.skip
    def test_2_camera_add(self):
        """添加摄像机"""
        self.page.table.add_btn.click()
        sleep(1)  # 等待弹窗完全出来
        self.page.addCamera.stylename.send_keys("DH-IPC-HEBDS001111")
        DropDownSelect(self.page.addCamera.Manufacturertype, "海康")
        sleep(1)
        self.page.addCamera.confirm.click()
        # 检查是否加成功
        sleep(3)  # 等待弹窗消失完毕
        self.page.searchTools.camera_input.send_keys('DH-IPC-HEBDS001111')
        DropDownSelect(self.page.searchTools.brand_brand_list, "海康")
        self.page.searchTools.search_btn.click()  # 点击检索
        sleep(2)
        search_result = self.page.table.get_row("DH-IPC-HEBDS001111")
        try:
            self.assertTrue(search_result, msg="找到摄像机型号，添加成功")
            self.log.info("添加摄像机用例执行成功")
        except Exception:
            self.log.error("添加摄像机用例执行失败", exc_info=True)
            save_screenshot('添加摄像机用例执行失败')
        self.page.searchTools.resert_btn.click()
        sleep(1)

    #@unittest.skip
    def test_3_camera_add(self):
        """修改摄像机"""
        self.page.searchTools.camera_input.send_keys('DH-IPC-HEBDS001111')  # 输入摄像机型号，和page页中对应是camera_input
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(2)
        result = self.page.table.get_row("DH-IPC-HEBDS001111")
        OperateSelect(result, "查看")
        sleep(2)
        self.page.modifyCamera.stylename.clear()
        sleep(1)
        self.page.modifyCamera.stylename.send_keys("DH-IPC-HEBDS002222")
        # self.page.modifyCamera.Manufacturertype.clear()
        sleep(2)
        DropDownSelect(self.page.modifyCamera.Manufacturertype, "大华")
        sleep(1)
        self.page.modifyCamera.confirm.click()
        # 检查是否修改成功
        sleep(1)  # 等待弹窗消失完毕
        self.page.searchTools.camera_input.clear()
        sleep(1)
        self.page.searchTools.camera_input.send_keys('DH-IPC-HEBDS002222')
        DropDownSelect(self.page.searchTools.brand_brand_list, "大华")
        self.page.searchTools.search_btn.click()  # 点击检索
        sleep(2)
        search_result = self.page.table.get_row("DH-IPC-HEBDS002222")
        try:
            self.assertTrue(search_result, msg="无法找到摄像机型号，查询失败")
            self.log.info("查询摄像机用例执行成功")
        except Exception:
            self.log.error("查询摄像机用例执行失败", exc_info=True)
            save_screenshot('查询摄像机用例执行失败')
        self.page.searchTools.resert_btn.click()
        sleep(1)

    #@unittest.skip
    def test_4_camera_add(self):
        """删除摄像机"""
        self.page.searchTools.camera_input.send_keys('DH-IPC-HEBDS002222')  # 输入摄像机型号，和page页中对应是camera_input
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(2)
        result = self.page.table.get_row("DH-IPC-HEBDS002222")
        OperateSelect(result, "删除")
        sleep(1)
        self.page.makeSuer.confirm.click()
        sleep(2)
        self.page.searchTools.resert_btn.click()
        delete_result = self.page.table.get_row("DH-IPC-HEBDS002222")
        try:
            self.assertFalse(delete_result, msg="删除失败，该摄像机依旧存在")
            self.log.info("删除摄像机用例执行成功")
        except Exception:
            self.log.error("删除摄像机用例执行失败", exc_info=True)
            save_screenshot('删除摄像机用例执行失败')

    #@unittest.skip
    def test_5_camera_search(self):
        """摄像机型号查询"""
        self.page.searchTools.resert_btn.click()
        self.page.searchTools.camera_input.send_keys('DH-IPC-HF81230E')  # 输入摄像机型号，和page页中对应是camera_input
        self.page.searchTools.search_btn.click()                 # 点击检索按钮
        sleep(2)

        result = self.page.table.get_row("DH-IPC-HF81230E")
        self.assertTrue(result, "通过摄像机型号:DH-IPC-HF81230E查询到了DH-IPC-HF81230E")
        self.page.searchTools.resert_btn.click()  # 点击重置按钮
        sleep(1)
        search_result = self.page.table.get_row("DH-IPC-HF81230E")
        try:
            self.assertTrue(search_result, msg="无法找到摄像机型号，查询失败")
            self.log.info("查询摄像机用例执行成功")
        except Exception:
            self.log.error("查询摄像机用例执行失败", exc_info=True)
            save_screenshot('查询摄像机用例执行失败')
        self.page.searchTools.resert_btn.click()
        sleep(1)


    #@unittest.skip
    def test_6_camera_search(self):
        """摄像机品牌查询"""
        sleep(2)
        self.page.searchTools.resert_btn.click()
        DropDownSelect(self.page.searchTools.brand_brand_list, "大华")      #输入大华相机型号
        self.page.searchTools.camera_input.send_keys('DH-IPC-HF81230E')
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(1)
        # result = self.page.table.get_row("大华")
        search_result = self.page.table.get_row("大华")
        try:
            self.assertTrue(search_result, msg="找到摄像机型号，查询成功")
            self.log.info("查询摄像机用例执行成功")
        except Exception:
            self.log.error("查询摄像机用例执行失败", exc_info=True)
            save_screenshot('查询摄像机用例执行失败')
        self.page.searchTools.resert_btn.click()
        sleep(1)

    #@unittest.skip
    def test_7_camera_search(self):
        """组合查询"""
        self.page.searchTools.resert_btn.click()
        self.page.searchTools.camera_input.send_keys('DH-IPC-HF81230E')
        sleep(2)
        DropDownSelect(self.page.searchTools.brand_brand_list, "大华")
        self.page.searchTools.search_btn.click()   #点击检索
        sleep(2)
        search_result = self.page.table.get_row("DH-IPC-HF81230E")
        try:
            self.assertTrue(search_result, msg="无法找到摄像机型号，查询失败")
            self.log.info("查询摄像机用例执行成功")
        except Exception:
            self.log.error("查询摄像机用例执行失败", exc_info=True)
            save_screenshot('查询摄像机用例执行失败')
        self.page.searchTools.resert_btn.click()
        sleep(1)

# # 厂商添加还在修改，暂时不写
#     def test_manu_add_8(self):
#         """厂商添加"""
#         self.page.table.add_btn1.click()
#         sleep(1)
#         self.page.button.click()
#         self.page.manuname.send_keys("测试001")
#         self.page.addManu.confirm.click()
#         sleep(2)


if __name__ == '__main__':
    unittest.main()